home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / c / stdlib / setenv.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-10  |  2.7 KB  |  96 lines

  1. /* 
  2.  * setenv.c --
  3.  *
  4.  *    Contains the source code for the "setenv" library procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/setenv.c,v 1.2 88/07/25 11:11:00 ouster Exp $ SPRITE (Berkeley)";
  18. #endif not lint
  19.  
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22.  
  23. extern char **environ;
  24.  
  25. /*
  26.  *----------------------------------------------------------------------
  27.  *
  28.  * setenv --
  29.  *
  30.  *    Associate the value "value" with the environment variable
  31.  *    "name" in this process's environment.
  32.  *
  33.  * Results:
  34.  *    None.
  35.  *
  36.  * Side effects:
  37.  *    The storage for the environment is modified.  If there already
  38.  *    was an environment variable by the given name, then it is
  39.  *    replaced.  Otherwise a new environment variable is created.
  40.  *    The new value will be visible to this process, and also will
  41.  *    be passed to children processes.
  42.  *
  43.  *----------------------------------------------------------------------
  44.  */
  45.  
  46. void
  47. setenv(name, value)
  48.     char *name;            /* Name of environment variable. */
  49.     char *value;        /* (New) value for variable. */
  50. {
  51.     register int    i;
  52.     register char **envPtr;
  53.     register char **newEnvPtr;
  54.     register char *charPtr;
  55.     register char *namePtr;
  56.     char *newEnvValue;
  57.  
  58.     newEnvValue = malloc ((unsigned) (strlen (name) + strlen (value) + 2));
  59.     if (newEnvValue == 0) {
  60.     return;
  61.     }
  62.     (void) sprintf(newEnvValue, "%s=%s", name, value);
  63.  
  64.     /*
  65.      * Although this procedure allocates new storage when necessary,
  66.      * it can't de-allocate the old storage, because it doesn't know
  67.      * which things were allocated with malloc and which things were
  68.      * allocated statically when the process was created.
  69.      */
  70.  
  71.     for (envPtr = environ, i=0; *envPtr != 0; envPtr++, i++) {
  72.     for (charPtr = *envPtr, namePtr = name;
  73.          *charPtr == *namePtr; namePtr++) {
  74.          charPtr++;
  75.          if (*charPtr == '=') {
  76.          namePtr++;
  77.          if (*namePtr == '\0') {
  78.              *envPtr = newEnvValue;
  79.              return;
  80.          }
  81.          break;
  82.          }
  83.      }
  84.     }
  85.     newEnvPtr = (char **) malloc ((unsigned) ((i + 2) * sizeof *newEnvPtr));
  86.     if (newEnvPtr == 0) {
  87.     return;
  88.     }
  89.     for (envPtr = environ, i = 0; *envPtr; envPtr++, i++) {
  90.     newEnvPtr[i] = *envPtr;
  91.     }
  92.     newEnvPtr[i] = newEnvValue;
  93.     newEnvPtr[i+1] = 0;
  94.     environ = newEnvPtr;
  95. }
  96.